package drytools.site.beans;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.faces.context.FacesContext;

import drytools.site.model.ObjectWithTags;
import dryworks.jsf.annotations.TagAnnotation;
import dryworks.jsf.examples.generator.GeneratorFileUtils;
import dryworks.jsf.examples.generator.GeneratorStringUtils;
import dryworks.jsf.examples.model.Example;
import dryworks.jsf.parsers.JSFAnnotatedExampleParser;

public class ExamplesInfoLoader implements IResourseVisitor {
	List<ObjectWithTags<Example>> allExamples = new ArrayList<ObjectWithTags<Example>>();

	public List<ObjectWithTags<Example>> getAllExamples() {
		return allExamples;
	}

	public void setAllExamples(List<ObjectWithTags<Example>> allExamples) {
		this.allExamples = allExamples;
	}

	@Override
	public void actionOnResource(String path) {
		String[] words = path.split("/");

		List<String> tagNamesFromPath = new ArrayList<String>();
		for (int i = 0; i < words.length - 1; i++) {
			if (words[i].equals("")) { // skip first slesh
				continue;
			}
			tagNamesFromPath.add(words[i]);
		}

		String exampleName = words[words.length - 1];
		if (exampleName.indexOf("_") != -1) {
			return;
		}
		if (!exampleName.endsWith(".jspx")) {
			return;
		}
		
		
		
		
		try {
			JSFAnnotatedExampleParser parser = new JSFAnnotatedExampleParser();
			InputStream in = FacesContext.getCurrentInstance()
					.getExternalContext().getResourceAsStream(path);
			BufferedReader rin = new BufferedReader(new InputStreamReader(in));
			Example example = parser.parse(rin);
			example.setPath(path);
			
			String exampleDir = words[words.length - 2];
			Set set = FacesContext.getCurrentInstance().getExternalContext().getResourcePaths("/javaSources/examplesbeans/drytools/icefaces/examplesbeans/"+exampleDir);
			for (Object object : set) {
				String subPath = (String)object;
				if (subPath.endsWith(".java")) {
					InputStream inSource = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream(subPath);
					//System.out.println("load java:"+subPath);
					String[] wordsInJavaPath = subPath.split("/");
					String javaShortName = wordsInJavaPath[wordsInJavaPath.length-1];
					String content = GeneratorFileUtils.getFileContent(inSource);
					example.getJavasources().put(javaShortName, content);
				}
			}
			// load faces-config
			
			String facesSegmentFilename = GeneratorStringUtils.upFirstLetter(exampleDir)+".xml1";
			String facesConfigPath = "/WEB-INF/examplesFacesConfigFiles/"+facesSegmentFilename;
			InputStream inFacesConfigSource = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream(facesConfigPath);
			String contentFacesConfig = GeneratorFileUtils.getFileContent(inFacesConfigSource);
			example.setFacesConfigPart(contentFacesConfig);
			
			ObjectWithTags<Example> exampleWithTags = new ObjectWithTags<Example>();
			exampleWithTags.setName(exampleName);
			exampleWithTags.setObject(example);
			
			exampleWithTags.getTags().addAll(tagNamesFromPath);
			allExamples.add(exampleWithTags);
		} catch (Exception e) {
			System.err.println("path:"+path);
			e.printStackTrace();
		}

		
	}
}
